<?php
namespace Home\Controller;
use Think\Controller;
class RegController extends CommonController {
    public function index(){
        
        /*链接数据库verify  D实例化模型*/
        $question = D('verify')->getOneVerify();
        //var_dump($question);
        
        //分配到模板 'question'可以在模板上输出验证码了
        $this->assign('question',$question);

        $this->assign('title','注册'); //分配动态的title，对应的是'注册'

        $this->display();
    }


    //检测推过来的数据
    public function checkData()
    {	//I方法自动转意
    	$password = I('post.password');
    	$repassword = I('post.repassword');

    	if ($password!=$repassword) {
    		$arr = array(
    			'error'=>1,
    			'info'=>'密码不一致',
    		);
    		echo json_encode($arr);
    		exit;
    	}

    	$verify = I('post.verify');
    	if ($verify!= $_SESSION['answer']) {
    		$arr = array(
    			'error'=>1,
    			'info'=>'人类验证失败',
    		);
    		echo json_encode($arr);
    		exit;
    	}

    	//把post的邮箱给checkEamil处理
    	if (D('user')->checkEmail(I('post.email'))) {
    		$arr = array(
    			'error'=>1,
    			'info'=>'邮箱已存在',
    		);
    		echo json_encode($arr);
    		exit; 
    	}

    	if (D('user')->checkNickname(I('post.nickname'))) {
    		$arr = array(
    			'error'=>1,
    			'info'=>'用户名已存在',
    		);
    		echo json_encode($arr);
    		exit; 
    	}

    	$data = I('post.');
    	$data['password'] = md5($password);
    	$data['create_time']=time();
        $ip = '61.140.139.83'; //模拟注册时访问者的IP  121.33.63.208  61.140.139.83
        $data['city'] = $this->getCity($ip); //模拟把获取的IP转换成具体城市存入数据库
        //$data['city'] = $this->getCity(get_client_ip()); //线上写法


    	if(M('user')->add($data)){
    		$arr = array(
    			'error'=>0,
    			'info'=>'注册成功',
    		);
    		echo json_encode($arr);
    		exit;
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>'注册失败',
    		);
    		echo json_encode($arr);
    		exit;
    	}


    }

    //网站未上线状态，模拟获取IP
    public function getCity($ip)
    {
       //$ip = '61.140.139.83'; //传过来的ip放参数位置

       $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
       //var_dump($url); //string 'http://ip.taobao.com/service/getIpInfo.php?ip=61.140.139.83'
       
       $data = file_get_contents($url); //采集-往url地址去访问 会响应内容回来类似异步 将整个文件读入一个字符串 返回带有json格式的字符串-如下↓
       //var_dump($data); //string '{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u5357","area_id":"800000","region":"\u5e7f\u4e1c\u7701","region_id":"440000","city":"\u5e7f\u5dde\u5e02","city_id":"440100","county":"","county_id":"-1","isp":"\u7535\u4fe1","isp_id":"100017","ip":"61.140.139.83"}}'
       
       $arr = json_decode($data,true); //json串转成数组 加true成数组array 不加就成json_decode的对象object
       /*var_dump($arr);
       array (size=2)
         'code' => int 0
         'data' => 
            array (size=13)
              'country' => string '中国' (length=6)
              'country_id' => string 'CN' (length=2)
              'area' => string '华南' (length=6)
              'area_id' => string '800000' (length=6)
              'region' => string '广东省' (length=9)
              'region_id' => string '440000' (length=6)
              'city' => string '广州市' (length=9)
              'city_id' => string '440100' (length=6)
              'county' => string '' (length=0)
              'county_id' => string '-1' (length=2)
              'isp' => string '电信' (length=6)
              'isp_id' => string '100017' (length=6)
              'ip' => string '61.140.139.83' (length=13)*/
        if($arr['code']==0){
            return $arr['data']['city'];
        }
       
    }

    //注册时获取访问者IP  有上线就可以用get_client_ip()获得访问者IP
    public function ip()
    {
        //var_dump($_SERVER); 
        $ip = get_client_ip(); //get_client_ip是TP封装好的方法可获取访问者IP
        var_dump($ip);  //string '127.0.0.1'
    }



}

?>